package org.andolphin.client;

import org.andolphin.client.Andolphin.Prefs;
import org.andolphin.client.service.AuthService;
import org.andolphin.client.service.IAuthService;
import org.andolphin.client.utils.DateUtils;
import org.andolphin.client.utils.StringUtils;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class MainActivity extends Activity implements OnItemClickListener {

    private static final String TAG = MainActivity.class.getName();

    private ArrayAdapter<CharSequence> adapter;

    private IAuthService service;

    private boolean bound = false;

    private ServiceConnection connection = new ServiceConnection() {

        public void onServiceConnected(ComponentName className, IBinder iservice) {
            service = IAuthService.Stub.asInterface(iservice);
            bound = true;
        }

        public void onServiceDisconnected(ComponentName className) {
            service = null;
            bound = false;
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        adapter = ArrayAdapter.createFromResource(this, R.array.main_menu_item_values, R.layout.main_menu_item);
        ListView mainMenu = (ListView) findViewById(R.id.main_menu);
        mainMenu.setAdapter(adapter);
        mainMenu.setOnItemClickListener(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (!bound) {
            try {
                this
                        .bindService(new Intent(MainActivity.this, AuthService.class), connection,
                                Context.BIND_AUTO_CREATE);
            } catch (Exception e) {
                Log.d(TAG, StringUtils.digMessage(e), e);
            }
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (bound) {
            try {
                this.unbindService(connection);
            } catch (Exception e) {
                Log.d(TAG, StringUtils.digMessage(e), e);
            }
        }
    }

    private boolean checkRegister() {
        SharedPreferences prefs = this.getSharedPreferences(Andolphin.Prefs.ANDOLPHIN, Prefs.MODE);
        String username = prefs.getString(Andolphin.Prefs.USERNAME, null);
        String password = prefs.getString(Andolphin.Prefs.PASSWORD, null);
        long expire = prefs.getLong(Andolphin.Prefs.EXPIRE, -1);

        if (username == null || password == null) {
            Intent intent = new Intent(Andolphin.Intent.LOGIN);
            intent.addCategory(Intent.CATEGORY_DEFAULT);
            this.startActivity(intent);
            return false;
        } else {
            if (DateUtils.nowInMillis() >= expire) {
                if (service != null) {
                    try {
                        service.doAuth();
                    } catch (RemoteException e) {
                        Log.e(TAG, StringUtils.digMessage(e), e);
                    }
                }
            }
            return true;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        super.onOptionsItemSelected(item);
        Intent intent;
        switch (item.getItemId()) {
        case R.id.config:
            intent = new Intent(Andolphin.Intent.CONFIG);
            intent.addCategory(Intent.CATEGORY_DEFAULT);
            this.startActivity(intent);
            break;
        case R.id.login:
            intent = new Intent(Andolphin.Intent.LOGIN);
            intent.addCategory(Intent.CATEGORY_DEFAULT);
            this.startActivity(intent);
            break;
        case R.id.register:
            intent = new Intent(Andolphin.Intent.REGISTER);
            intent.addCategory(Intent.CATEGORY_DEFAULT);
            this.startActivity(intent);
            break;
        case R.id.exit:
            doSystemExit();
            break;
        }

        return false;
    }

    public void onItemClick(AdapterView<?> parent, View v, int pos, long id) {
        if (checkRegister()) {
            Intent intent;
            switch (pos) {
            case 0:
                intent = new Intent(Andolphin.Intent.PAYMENT);
                intent.addCategory(Intent.CATEGORY_DEFAULT);
                this.startActivity(intent);
                break;
            case 1:
                intent = new Intent(Andolphin.Intent.NOTE);
                intent.addCategory(Intent.CATEGORY_DEFAULT);
                this.startActivity(intent);
                break;
            case 2:
                intent = new Intent(Andolphin.Intent.WORD);
                intent.addCategory(Intent.CATEGORY_DEFAULT);
                this.startActivity(intent);
                break;
            case 3:
                intent = new Intent(Andolphin.Intent.BIRTHDAY);
                intent.addCategory(Intent.CATEGORY_DEFAULT);
                this.startActivity(intent);
                break;
            default:
                break;
            }
        }
    }

    private void doSystemExit() {
        this.finish();
    }

}